import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 数据处理对象
 */
public class UserDao {
    /**
     * 按名字查
     * 
     * @param con
     * @param pstmt
     * @param name
     * @return
     * @throws Exception
     */
    public ResultSet queryUser(Connection con, PreparedStatement pstmt, String name) throws Exception {
        String sql = "select * from user where username=?";
        // PreparedStatement pstmt = null;
        ResultSet rs = null;
        pstmt = con.prepareStatement(sql);
        pstmt.setString(1, name);
        // 执行sql
        rs = pstmt.executeQuery();
        // pstmt.close();
        return rs;
    }

    /**
     * 查询数据库表中的所有记录
     * @param con
     * @param pstmt
     * @param user
     * @return
     * @throws Exception
     */
    public ResultSet queryUserAll(Connection con, PreparedStatement pstmt) throws Exception {
        String sql = "select id,username,password,age from user";
        ResultSet rs = null;
        System.out.println(pstmt);
        pstmt = con.prepareStatement(sql);//查所有记录的时候就不用setXXX了。
        System.out.println(pstmt);

        rs = pstmt.executeQuery();
        return rs;
    }
}
